package com.hzhc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzhc.entity.CntrMain;
import com.hzhc.mapper.CntrMainMapper;
import com.hzhc.service.CntrMainService;
import com.hzhc.utils.ResultUtil;
import com.hzhc.vo.CntrMainVo;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 箱主信息 服务实现类
 * </p>
 *
 * @author 功不唐捐
 * @since 2024-05-23
 */
@Service
public class CntrMainServiceImpl extends ServiceImpl<CntrMainMapper, CntrMain> implements CntrMainService {

    @Override
    public List<CntrMainVo> getCntrMainList(Page<CntrMainVo> page, CntrMainVo vo) {
        IPage<CntrMainVo> iPage = baseMapper.getCntrMainList(page,vo);
        return iPage.getRecords();
    }

    @Override
    public ResultUtil<T> addCntrMain(CntrMain main) {
        boolean b = save(main);
        if (b){
            return ResultUtil.ok();
        }
        return ResultUtil.fail();
    }

    @Override
    public ResultUtil<T> updateCntrMian(CntrMain main) {
        boolean b = updateById(main);
        if (b){
            return ResultUtil.ok();
        }
        return ResultUtil.fail();
    }

    @Override
    public ResultUtil<T> delCntrMain(Long id) {
        CntrMain main = getById(id);
        main.setDisable(1);
        boolean b = updateById(main);
        if (b){
            return ResultUtil.ok();
        }
        return ResultUtil.fail();
    }

    @Override
    public Boolean checkMainName(CntrMain main) {
        LambdaQueryWrapper<CntrMain> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CntrMain::getCntrMain,main.getCntrMain())
                .eq(CntrMain::getDisable,0);
        if (main.getId() != null){
            wrapper.ne(CntrMain::getId,main.getId());
        }
        int num = count(wrapper);
        return num <= 0;
    }

    @Override
    public List<CntrMainVo> getCntrMain(CntrMainVo vo) {
        return baseMapper.getCntrMain(vo);
    }
}
